home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / VerticalString-c / VerticalString.µ.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  7.9 KB  |  342 lines  |  [TEXT/MMCC]

  1. // VerticalText.c
  2.  
  3. #define over qd.screenBits.bounds.right
  4. #define down qd.screenBits.bounds.bottom
  5.  
  6. MenuHandle    appleMenu, fileMenu, editMenu;
  7.  
  8. enum {
  9.     appleID = 1,
  10.     fileID,
  11.     editID
  12. };
  13.  
  14. enum {
  15.     openItem = 1,
  16.     closeItem,
  17.     quitItem = 4
  18. };
  19.  
  20.  
  21. WindowPtr    shell_window, about_window;
  22. Rect        dragRect;
  23. Rect        windowBounds, about_bounds; 
  24. Rect        strRect ={ 5, 20, 364, 40 };
  25. short            width = 1;
  26.  
  27. // Prototypes.
  28.  
  29. void SetUpWindow (void);
  30. void DoAbout (void);
  31. void SetUpMenus (void);
  32. void VerticalString (Rect word_rect, Str255 word_string);
  33. void StyleTheText (short whatFont, short whatStyle, short whatSize);
  34. void DrawTheStrings (short active);
  35. void AdjustMenus (void);
  36. static enable (MenuHandle menu, short item, short ok);
  37. void HandleMenu (long mSelect);
  38. void InitMacintosh (void);
  39. void HandleMouseDown (EventRecord *theEvent);
  40. void HandleEvent (void);
  41. void main (void);
  42.  
  43. void SetUpWindow (void)
  44. {
  45.     dragRect = qd.screenBits.bounds;
  46.     
  47.     SetRect (&windowBounds, 2, 22, over - 4, down - 4);
  48.     
  49.     shell_window = NewWindow (0L, &windowBounds, "\pKen's Shell", true, 
  50.                               plainDBox, (WindowPtr) -1L, true, 0);
  51.     SetPort (shell_window);
  52. }
  53.  
  54. void SetUpMenus (void)
  55. {
  56.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  57.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  58.     InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
  59.     DrawMenuBar ();
  60.     AppendMenu (appleMenu, "\pAbout VerticalString");
  61.     AddResMenu (appleMenu, 'DRVR');
  62.     AppendMenu (fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
  63.     AppendMenu (editMenu, "\pUndo/Z; (-;Cut/X;Copy/C;Paste/V;Clear");
  64. }
  65.  
  66. void VerticalString (Rect word_rect, Str255 word_string)
  67. {
  68.     #define center 5    // center of label is 'center' pixels from left of rect.
  69.     
  70.     short numChars, leading, stringCenter, x, curVert;
  71.     FontInfo fInfo;
  72.     
  73.     GetFontInfo (&fInfo);
  74.     leading = fInfo.ascent + fInfo.descent + fInfo.leading;
  75.     stringCenter = word_rect.left - center;
  76.  
  77. //    ForeColor (redColor);    //• Color here and not elsewhere for same.
  78.     curVert = word_rect.top + leading;    // primed for first character.
  79.     numChars = strlen (word_string);
  80.     for (x = 1; x < numChars; x++)
  81.     {
  82.         MoveTo (stringCenter - (CharWidth (word_string[x]) / 2), 
  83.                 curVert);
  84.         DrawChar ((char) word_string[x]);
  85.         curVert = curVert + leading;
  86.     }
  87. }
  88.  
  89. void StyleTheText (short whatFont, short whatStyle, short whatSize)
  90. {
  91.     TextFont (whatFont);
  92.     TextFace (whatStyle);
  93.     TextSize (whatSize);
  94. }
  95.  
  96. void DrawTheStrings (short active)
  97. {
  98.     short        color = true;
  99.     
  100.     SetPort (shell_window);
  101.     
  102.     ForeColor (blueColor);    // oldStyle color.
  103.     StyleTheText (monaco, 0, 9);
  104.     VerticalString (strRect, "\pMy name is Long but this is not\0");
  105.     OffsetRect (&strRect, 20, 0);
  106.     VerticalString (strRect, "\pChinese writing.");
  107.     
  108.     ForeColor (redColor);
  109.     StyleTheText (newYork, outline, 14);
  110.     OffsetRect (&strRect, 40, 0);
  111.     VerticalString (strRect, "\pSuperman\0");
  112.     
  113.     ForeColor (blackColor);
  114.     StyleTheText (monaco, 0, 9);
  115.     OffsetRect (&strRect, 20, 0);
  116.     VerticalString (strRect, "\pStrange visitor from another\0");
  117.     OffsetRect (&strRect, 20, 0);
  118.     VerticalString (strRect, "\pplanet who came to Earth with\0");
  119.     OffsetRect (&strRect, 20, 0);
  120.     VerticalString (strRect, "\ppowers and abilities far beyond\0");
  121.     OffsetRect (&strRect, 20, 0);
  122.     VerticalString (strRect, "\pthose of mortal men. Then went\0");
  123.     StyleTheText (0, 0, 12);
  124.     OffsetRect (&strRect, 20, 0);
  125.     VerticalString (strRect, "\pto Chicago!\0");
  126.     
  127.     ForeColor (magentaColor);
  128.     StyleTheText (monaco, 0, 9);
  129.     OffsetRect (&strRect, 40, 0);
  130.     VerticalString (strRect, "\pThe VerticalString routine came\0");
  131.     OffsetRect (&strRect, 20, 0);
  132.     VerticalString (strRect, "\pfrom SplatMaster Pascal source,\0");
  133.     OffsetRect (&strRect, 20, 0);
  134.     VerticalString (strRect, "\pby Jon Benton, and ported to C\0");
  135.     OffsetRect (&strRect, 20, 0);
  136.     VerticalString (strRect, "\pby Kenneth A. Long in mid 1992.\0");
  137.     OffsetRect (&strRect, 20, 0);
  138.     VerticalString (strRect, "\pIn May, 1994 it was put in this\0");
  139.     OffsetRect (&strRect, 20, 0);
  140.     VerticalString (strRect, "\pdemo, for YOUR enjoyment as an.\0");
  141.  
  142.     StyleTheText (monaco, bold, 9);
  143.     OffsetRect (&strRect, 20, 0);
  144.     VerticalString (strRect, "\pitty bitty bytes(tm) product.\0");
  145.     
  146.     StyleTheText (monaco, 0, 9);
  147.     ForeColor (greenColor);
  148.     OffsetRect (&strRect, 40, 0);
  149.     VerticalString (strRect, "\pVertical strings are excellent\0");
  150.     OffsetRect (&strRect, 20, 0);
  151.     VerticalString (strRect, "\pfor labeling vertical scrolls.\0");
  152.     
  153.     ForeColor (blackColor);
  154.     OffsetRect (&strRect, 40, 0);
  155.     VerticalString (strRect, "\pBullseye was used for the shell.\0");
  156.     OffsetRect (&strRect, 20, 0);
  157.     VerticalString (strRect, "\pJon Benton wrote SplatMaster.\0");
  158.     OffsetRect (&strRect, 20, 0);
  159.     VerticalString (strRect, "\pVertical String Demo is PD.\0");
  160.     StyleTheText (0, 0, 12);
  161. }
  162.  
  163. void AdjustMenus (void)
  164. {
  165.     register WindowPeek wp = (WindowPeek) FrontWindow ();
  166.     short kind = wp ? wp->windowKind : 0;
  167.     Boolean DA = kind < 0;
  168.     
  169.     enable (editMenu, 1, DA);
  170.     enable (editMenu, 3, DA);
  171.     enable (editMenu, 4, DA);
  172.     enable (editMenu, 5, DA);
  173.     enable (editMenu, 6, DA);
  174.     
  175.     enable (fileMenu, openItem, ! ((WindowPeek) shell_window)->visible);
  176.     enable (fileMenu, closeItem, DA || ((WindowPeek) shell_window)->visible);
  177. }
  178.  
  179. static enable (MenuHandle menu, short item, short ok)
  180. {
  181.     if (ok)
  182.         EnableItem (menu, item);
  183.     else
  184.         DisableItem (menu, item);
  185. }
  186.  
  187. void HandleMenu (long mSelect)
  188. {
  189.     short        menuID = HiWord (mSelect);
  190.     short        menuItem = LoWord (mSelect);
  191.     Str255        name;
  192.     GrafPtr        savePort;
  193.     WindowPeek    frontWindow;
  194.     
  195.     switch (menuID)
  196.     {
  197.         case    appleID:
  198.             if (menuItem == 1)
  199.                 ;    //• Put your DoAboutBox call here.
  200.             else
  201.                 {
  202.                     GetPort (&savePort);
  203.                     GetItem (appleMenu, menuItem, name);
  204.                     OpenDeskAcc (name);
  205.                     SetPort (savePort);
  206.             }
  207.         break;
  208.         
  209.         case    fileID:
  210.             switch (menuItem)
  211.             {
  212.                 case    openItem:
  213.                     ShowWindow (shell_window);
  214.                     SelectWindow (shell_window);
  215.                 break;
  216.                               
  217.                 case    closeItem:
  218.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  219.                 break;
  220.             
  221.                 if (frontWindow->windowKind < 0)
  222.                     CloseDeskAcc (frontWindow->windowKind);
  223.                 else 
  224.                     if (frontWindow = (WindowPeek) shell_window)
  225.                         HideWindow (shell_window);
  226.                       break;
  227.                       
  228.                 case    quitItem:
  229.                     ExitToShell ();
  230.                 break;
  231.             }
  232.         break;
  233.                   
  234.         case    editID:
  235.             if (!SystemEdit (menuItem-1))
  236.                 SysBeep (5);
  237.         break;
  238.     }
  239. }
  240.  
  241. void InitMacintosh (void)
  242. {
  243.     MaxApplZone ();
  244.     
  245.     InitGraf (& (qd.thePort));
  246.     InitFonts ();
  247.     FlushEvents (everyEvent, 0);
  248.     InitWindows ();
  249.     InitMenus ();
  250.     TEInit ();
  251.     InitDialogs (0L);
  252.     InitCursor ();
  253. }
  254.  
  255. void HandleMouseDown (EventRecord    *theEvent)
  256. {
  257.     WindowPtr    theWindow;
  258.     short            windowCode = FindWindow (theEvent->where, &theWindow);
  259.     
  260.     switch (windowCode)
  261.     {
  262.         case inSysWindow: 
  263.             SystemClick (theEvent, theWindow);
  264.         break;
  265.     
  266.         case inMenuBar:
  267.             AdjustMenus ();
  268.             HandleMenu (MenuSelect (theEvent->where));
  269.         break;
  270.     
  271.         case inDrag:
  272.             if (theWindow == shell_window)
  273.                 DragWindow (shell_window, theEvent->where, &dragRect);
  274.         break;
  275.         
  276.         case inContent:
  277. //            if (theWindow == shell_window)
  278. //            {
  279. //                if (theWindow != FrontWindow ())
  280. //                    SelectWindow (shell_window);
  281. //                else
  282. //                    InvalRect (&shell_window->portRect);
  283. //            }
  284.         break;
  285.         
  286.         case inGoAway:
  287.             if (theWindow == shell_window && 
  288.                 TrackGoAway (shell_window, theEvent->where))
  289.                 HideWindow (shell_window);
  290.         break;
  291.     }
  292. }
  293.  
  294. void HandleEvent (void)
  295. {
  296.     short            ok;
  297.     EventRecord    theEvent;
  298.  
  299.     HiliteMenu (0);
  300.     SystemTask ();        /* Handle desk accessories */
  301.     
  302.     ok = GetNextEvent (everyEvent, &theEvent);
  303.     if (ok)
  304.     switch (theEvent.what)
  305.     {
  306.         case mouseDown:
  307.             HandleMouseDown (&theEvent);
  308.         break;
  309.             
  310.         case keyDown: 
  311.         case autoKey:
  312.         if ((theEvent.modifiers & cmdKey) != 0)
  313.         {
  314.             AdjustMenus ();
  315.             HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  316.         }
  317.         break;
  318.             
  319.         case updateEvt:
  320.             BeginUpdate (shell_window);
  321.                 EraseRect (&windowBounds);
  322.                 DrawTheStrings (((WindowPeek) shell_window)->hilited);
  323.             EndUpdate (shell_window);
  324.         break;
  325.         
  326.         case activateEvt:
  327.             InvalRect (&shell_window->portRect);
  328.         break;
  329.     }
  330. }
  331.  
  332. void main ()
  333. {
  334.     InitMacintosh ();
  335.     SetUpMenus ();
  336.     SetUpWindow ();
  337.     
  338.     for (;;)
  339.         HandleEvent ();
  340. }
  341.  
  342.